<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>自动更新</title>
  <style>
    html,
    body {
      font: caption;
      height: 100%;
      width: 100%;
      margin: 0;
      padding: 0;
      background: #FFF;
      -webkit-app-region: drag;
      background-image: url('./bg.png');
      background-position: center;
      background-size: contain;
      background-repeat: no-repeat;
    }

    * {
      box-sizing: border-box;
    }

    .container {
      padding: 0 36px;
      overflow: hidden;
    }

    .title {
      font-size: 24px;
      line-height: 26px;
      color: #333;
      margin-top: 54px;
      margin-bottom: 6px;
      -webkit-app-region: no-drag;
    }

    .version {
      display: inline-block;
      min-width: 44px;
      height: 18px;
      line-height: 18px;
      background-color: #F1F5F7;
      border-radius: 4px;
      text-align: center;
      padding: 0 6px;
      color: #999;
      font-size: 12px;
      margin-bottom: 36px;
      -webkit-app-region: no-drag;
    }

    .desc {
      color: #333;
      white-space: pre-line;
      line-height: 2em;
      height: 100px;
      overflow: auto;
      -webkit-app-region: no-drag;
    }

    .desc::-webkit-scrollbar {
      width: 6px;
    }

    .desc::-webkit-scrollbar-thumb {
      border-radius: 10px;
      box-shadow: inset 0 0 5px rgba(0, 0, 0, 0);
      background: rgba(125, 125, 125, 0);
    }

    .desc:hover::-webkit-scrollbar-thumb {
      box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.1);
      background: rgba(125, 125, 125, 0.42);
    }

    .footer-wrap {
      position: absolute;
      width: 100%;
      left: 0;
      padding: 0 36px;
      bottom: 24px;
      color: #5DACF8;
      height: 60px;
    }

    .footer-wrap button {
      outline: none;
      width: 100%;
      height: 32px;
      color: #FFF;
      background-color: #5CACF8;
      border: solid 1px #5CACF8;
      border-radius: 4px;
      margin-bottom: 12px;
      -webkit-app-region: no-drag;
      cursor: pointer;
    }

    .footer-wrap button:active {
      background-color: #5CB2F8;
    }

    .footer-wrap a {
      line-height: 12px;
      color: #BBB;
      cursor: pointer;
      -webkit-app-region: no-drag;
    }

    .footer-wrap .cancel {
      float: right;
    }

    .process {
      width: 100%;
      height: 6px;
      background-color: #E4E5E5;
      border-radius: 3px;
      margin-top: 6px;
      overflow: hidden;
    }

    .process .process-inner {
      height: 100%;
      background: #5CACF8;
      border-radius: 3px;
    }

    .window-close {
      display: none;
      -webkit-app-region: no-drag;
    }

    .window-close.osx {
      position: absolute;
      left: 8px;
      top: 12px;
      z-index: 100;
      vertical-align: middle;
      width: 12px;
      height: 12px;
      line-height: 12px;
      font-size: 12px;
      display: inline-block;
      color: #333;
      text-align: center;
      cursor: pointer;
      border-radius: 50%;
      font-weight: 500;
      background: #FF6058;
    }

    .window-close.osx>span {
      display: none;
    }

    .window-close.win {
      position: absolute;
      right: 0;
      top: 0;
      z-index: 100;
      vertical-align: middle;
      width: 40px;
      height: 30px;
      line-height: 30px;
      font-size: 14px;
      display: inline-block;
      color: #333;
      text-align: center;
      cursor: pointer;
      background-color: #FFF;
      background-image: url('./close.png');
      background-position: center;
      background-size: 30%;
      background-repeat: no-repeat;
    }

    .window-close.win:active {
      background-color: #E4E5E5;
    }
  </style>
</head>

<body>
  <div id='container' class="container">
    <div id='close' class="window-close"></div>
    <div class="title">electron-react</div>
    <div id='version' class="version">v2.2.2</div>
    <div id='desc' class="desc"> - </div>

    <div id='updateWrap' class="footer-wrap">
      <button id='update'>立即更新</button>
      <a id='ignore'>忽略</a>
    </div>

    <div id='downloadWrap' class="footer-wrap" style="display: none;">
      <div>
        <span id='percent' class="percent">70%</span>
        <a id='cancel' class='cancel'>取消</a>
      </div>
      <div class="process">
        <div id='processInner' class="process-inner" style="width: 70%;"></div>
      </div>
    </div>
  </div>
  <script>
    (() => {
      const md5File = require('md5-file');
      const url = require('url');
      const path = require('path');
      const { remote, ipcRenderer } = require('electron');

      const basePath = remote.getGlobal('__dirname');
      const Download = require('./../utils/download.js');

      const { query } = url.parse(window.location.href, true);
      const { pathname } = url.parse(query.updateUrl);
      const filename = pathname.split('/').pop();

      $ = Array.from(document.querySelectorAll('[id]')).reduce((v, dom) => {
        v[dom.id] = dom;
        return v;
      }, {});
      let data = query;
      const update = (params) => {
        data = Object.assign(data, params);
        $.version.innerHTML = `v${data.version}`;
        $.desc.innerHTML = data.updateMessage || '-';
        $.processInner.style.width = data.percent + '%';
        $.percent.innerHTML = data.percent + '%';

        if (data.downloading) {
          $.updateWrap.style.display = 'none';
          $.downloadWrap.style.display = 'block';
        } else {
          $.updateWrap.style.display = 'block';
          $.downloadWrap.style.display = 'none';
        }

        if (data.forceUpdate === 'true') {
          $.cancel.style.display = 'none';
          $.ignore.style.display = 'none';
        }
      };
      const bindEvent = () => {
        ipcRenderer.on('autoUpdater-update', (event, newData) => {
          if (!data.downloading) {
            update(newData);
          }
        });
        if (process.platform === 'darwin') {
          $.close.classList.add('osx');
        } else {
          $.close.classList.add('win');
        }
        $.close.onclick = () => remote.getCurrentWindow().close();
        $.ignore.onclick = () => ipcRenderer.send('autoUpdater-ignore');

        $.update.onclick = () => {
          update({
            downloading: true,
            percent: 0
          });
          Download({
            url: query.updateUrl,
            name: filename,
            progress: (size, current) => update({ percent: (current / size * 100).toFixed(1) }),
          }).then(({ promise, abort }) => {
            $.cancel.onclick = () => {
              update({ downloading: false });
              abort();
              ipcRenderer.send('autoUpdater-ignore');
            };
            return promise
          }).then(({ name }) => {
            md5File(name, (err, hash) => {
              if (err) {
                ipcRenderer.send('autoUpdater-downloaded', new Error(err));
              } else {
                $.percent.innerHTML = '正在安装';
                $.cancel.onclick = null;
                $.cancel.style.visibility = 'hidden';
                ipcRenderer.send('autoUpdater-downloaded', null, name, hash);
              }
            });
          }).catch(() => {
            if (data.downloading) {
              ipcRenderer.send('autoUpdater-downloaded', new Error('下载更新包失败'));
              update({ downloading: false });
            }
          });
        };
      };

      bindEvent();
      update();

    })();

  </script>
</body>

</html>